<?php
namespace App\Services;

use App\Models\InvestmentMeeting;
use App\Models\Manufacturer;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;

class ImageServices
{
    public function __construct()
    {

    }

    public static function deleteFolder($folderPath)
    {
        if (is_dir($folderPath)) {
            $files = array_diff(scandir($folderPath), array('.', '..'));
            foreach ($files as $file) {
                $filePath = $folderPath . '/' . $file;
                is_dir($filePath) ? self::deleteFolder($filePath) : @unlink($filePath);
            }
            @rmdir($folderPath);
        }
    }

    /**
     * 生成邀请函
     * @param
     * @return
     * @author ScorpionFong
     */
    public static function buildInvite(InvestmentMeeting $info, Manufacturer $manufacturer)
    {
        // 887 * 1920
        $img = Image::make(storage_path('app/public/bgimg/20241128/2.png'));
        $textConent = $manufacturer->title;
        $img->text($textConent, 443, 710, function ($font) use ($textConent) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(Str::length($textConent) > 9 ? 50 : 65);
            $font->color('#FFE86E');
            $font->align('center');
        });
        $textConent = $info->title;
        $img->text($textConent, 443, 950, function ($font) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(70);
            $font->color('#FFE86E');
            $font->align('center');
        });
        $textConent = date('Y-m-d H:i', strtotime($info->meeting_time));
        $img->text($textConent, 245, 1034, function ($font) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(38);
            $font->color('#FFE86E');
            $font->align('left');
        });
        $textConent = $info->entry_time;
        $img->text($textConent, 245, 1106, function ($font) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(38);
            $font->color('#FFE86E');
            $font->align('left');
        });
        $textConent = $info->address;
        $img->text($textConent, 245, 1177, function ($font) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(35);
            $font->color('#FFE86E');
            $font->align('left');
        });
        if ($info->full_address) {
            // $img->text('详细地址：', 50, 1740, function ($font) {
            //     $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            //     $font->size(30);
            //     $font->color('#FFE86E');
            //     $font->align('left');
            // });
            foreach (mb_str_split($info->full_address, 20) as $k => $v) {
                $img->text($v, 245, 1240 + 35 * $k, function ($font) {
                    $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
                    $font->size(30);
                    $font->color('#FFE86E');
                    $font->align('left');
                });
            }
        }
        @mkdir(storage_path('app/public/invite/' . $info->title . '_' . $info->id . '/' . $manufacturer->title . '_' . $manufacturer->id), 0777, true);
        $path = '/invite/' . $info->title . '_' . $info->id . '/' . $manufacturer->title . '_' . $manufacturer->id . '/' . ($manufacturer->person ? $manufacturer->person->name : '') . '-' . $manufacturer->name . '-邀请函.png';
        $img->save(storage_path('app/public' . $path));
        return $path;
    }

    /**
     * 生成手卡
     * @param
     * @return
     * @author ScorpionFong
     */
    public static function buildCard(InvestmentMeeting $meeting, Manufacturer $info)
    {
        $img = Image::make(storage_path('app/' . ($meeting->hand_card_img ?? 'public/bgimg/20241128/1.png')));
        $textConent = $info->title;
        $img->text($textConent, 122, 550, function ($font) use ($textConent) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(Str::length($textConent) > 9 ? 45 : 60);
            $font->color('#FFE86E');
            $font->align('left');
        });
        if ($info->category) {
            $textConent = $info->category->name;
            $img->text($textConent, 122, 722, function ($font) {
                $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
                $font->size(60);
                $font->color('#FFE86E');
                $font->align('left');
            });
        }
        if ($info->person) {
            $textConent = $info->person->nickname;
            $img->text($textConent, 122, 1228, function ($font) {
                $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
                $font->size(60);
                $font->color('#FFE86E');
                $font->align('left');
            });
        }
        $textConent = $info->name;
        $img->text($textConent, 122, 890, function ($font) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(60);
            $font->color('#FFE86E');
            $font->align('left');
        });
        $textConent = $info->mobile;
        $img->text($textConent, 122, 1056, function ($font) {
            $font->file(storage_path('app/public/font/PingFang Heavy.ttf'));
            $font->size(60);
            $font->color('#FFE86E');
            $font->align('left');
        });
        @mkdir(storage_path('app/public/invite/' . $meeting->title . '_' . $info->investment_meeting_id . '/' . $info->title . '_' . $info->id), 0777, true);
        $path = '/invite/' . $meeting->title . '_' . $info->investment_meeting_id . '/' . $info->title . '_' . $info->id . '/' . ($info->person ? $info->person->name : '') . '-' . $info->name . '-手卡.png';
        $img->save(storage_path('app/public' . $path));
        return $path;
    }
}
